<?php
class Application {
	private $_controllerName = null;
	/**
	 * Controller
	 * 
	 * @var BaseController
	 */
	private $_controller = null;
	public function __construct() {
		$this->init();
		if (isset ( $_REQUEST [Config::CONTROLLER_PARRAM_NAME] ))
			$this->_controllerName = $_REQUEST [Config::CONTROLLER_PARRAM_NAME];
		else {
			$this->_controllerName = Config::CONTROLLER_DEFAULT;
		}
	}
	public function init(){
	}
	public function run() {
		try {
			$this->_controller = ControllerFactory::instance ( $this->_controllerName );
			$this->_controller->setContext ( $this->_controllerName );
			$this->beforeRun();
			$this->_controller->run ();
		} catch ( ControllerFactoryException $e ) {
			echo $e->getMessage ();
		} catch ( Exception $e ) {
			echo $e->getMessage ();
		}
	}
	public function beforeRun(){
		if(isset($_GET['_view']) && ($_GET['_view'] == 'json' || $_GET['_view'] == 'array'))
			return ;
		session_start();
		if(Auth::getInstance() == null){
			$this->_controllerName = "User";
			$this->_controller = ControllerFactory::instance ( $this->_controllerName );
			$this->_controller->setContext ( $this->_controllerName );
			$this->_controller->setActionName('login');
			return ;
		}
	}
	/**
	 * @return the $_controller
	 */
	public function getController() {
		return $this->_controller;
	}
}